package com.simon.mctm.biz.sys.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import com.simon.mctm.base.response.DoResponse;
import com.simon.mctm.base.response.PageResponse;
import com.simon.mctm.exception.CheckException;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.token.TokenValidate;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Permission;
import com.simon.mctm.biz.sys.service.PermissionService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;


/**
 * <p>
 * 菜单&权限 前端控制器
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@RestController
@Api(description = "菜单&权限控制器")
@RequestMapping("/sys/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    @ApiOperation(value = "添加菜单&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "permission", value = "菜单&权限对象", required = true, dataType = "Permission")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addPermission(@RequestBody Permission permission) {
        Permission dPermission = permissionService.selectOne(
                new EntityWrapper<Permission>().eq("code", permission.getCode())
                        .eq("pcode", permission.getPcode())
        );
        if (dPermission != null) {
            throw new OperationFailedException("该菜单/权限已存在");
        } else {
            boolean res = permissionService.insert(permission);
            if (res) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException("添加菜单&权限失败");
            }
        }
    }


    @ApiOperation(value = "获取指定菜单&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单&权限id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getPermission(@RequestParam Integer id) {
        Permission permission = permissionService.getPermissionById(id);
        if (permission != null) {
            return new DoResponse<>(permission);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取菜单&权限列表")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getAllPermission() {
        List<Permission> permissionList = permissionService.getPermissionList();
        if (!CollectionUtils.isEmpty(permissionList)) {
            return new DoResponse<>(permissionList);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除菜单&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单&权限id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deletePermission(@RequestParam Integer id) {
        boolean res = permissionService.deleteById(id);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新菜单&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单&权限id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "permission", value = "菜单&权限对象", required = true, dataType = "Permission")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updatePermission(@RequestParam Integer id, @RequestBody Permission permission) {
        Permission temp = permissionService.selectById(id);
        if (temp == null) {
            throw new OperationFailedException("该菜单&权限不存在");
        }
        Permission temp1 = permissionService.selectOne(
                new EntityWrapper<Permission>().eq("code", permission.getCode())
                        .eq("pcode", permission.getPcode())
        );
        if (temp1 != null) {
            throw new OperationFailedException("该菜单/权限的编码已存在，不允许重复");
        }
        boolean res = permissionService.updateById(permission);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "根据用户获取权限", notes = "根用户id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get/user", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getPermissionByUserId(@RequestParam Integer userId) {
        List<Permission> permissionList = permissionService.findByUserId(userId);
        if (!CollectionUtils.isEmpty(permissionList)) {
            return new DoResponse<>(permissionList);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "根据角色获取权限", notes = "根角色id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get/role", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getPermissionByRoleId(@RequestParam Integer roleId) {
        List<Permission> permissionList = permissionService.findByRoleId(roleId);
        if (!CollectionUtils.isEmpty(permissionList)) {
            return new DoResponse<>(permissionList);
        } else {
            throw new OperationFailedException();
        }
    }
}

